package com.binarycube.webserver.datasource;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;

import com.binarycube.webserver.config.DatasourceConfig;

public class FolderDataSource extends AbstractDataSource implements IDataSource{
	static Logger log = Logger.getLogger(FolderDataSource.class.getName());
	
	String folder;
	
	public List<Record> doQuery() {
		validateParams();
		File root = new File(folder);
		if (root.isDirectory()) {
			for (File file : root.listFiles()) {
				Record r = Record.MappedRecord();
				r.put("name", file.getName());
				r.put("path", file.getPath());
				r.put("type", file.isDirectory() ? "DIR" : "FILE");
				_records.add(r);
				
			}
		}
		return _records;
	}
	
	public void configure(DatasourceConfig config) {
		super.configure(config);
		
		folder = _config.getProperty("folder");
		File root = new File(folder);
		if (!root.exists()) log.error("Error initialising Datasource: " + _config.getName() + " path not found");
		

	}

}
